package org.jsrml.core.supplier.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.base.entity.EmAddress;
import org.jsrml.core.supplier.command.DoVendorStatusCo;
import org.jsrml.core.supplier.command.SaveVendorCo;

@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "VENDOR")
@SuppressWarnings("serial")
public class Vendor extends StringIdBaseEntity {

	private VendorInfo info;
	
	/**
	 * 地址
	 */
	private EmAddress address;
	
	
	private BaseStatus status;

	/**
	 * 供应商状态
	 */
	private VendorStatus vendorStatus;
	
	/**
	 * 处理工艺上状态
	 * @param command
	 */
	public void doVendorStatus(DoVendorStatusCo command){
		getVendorStatus().setIsClose(0);
		getVendorStatus().setClosedBy(command.getClosedBy());
		getVendorStatus().setClosedDate(new Date());
		getVendorStatus().setLevel(command.getLevel());
		getVendorStatus().setAcessBy(command.getAcessBy());
		getVendorStatus().setAcessCode(command.getAcessCode());
		getVendorStatus().setAcessDate(command.getAcessDate());
		getVendorStatus().setIsAcess(command.getIsAcess());
		
	}
	
	public void create(SaveVendorCo command){
		setId(UUIDGenerator.getUUID());
		
		if(info == null){
			setInfo(new VendorInfo());
		}
		getInfo().setVendorCode(command.getVendorCode());
		getInfo().setVendorName(command.getVendorName());
		getInfo().setFax(command.getFax());
		getInfo().setPostCode(command.getPostCode());
		getInfo().setTel(command.getTel());
		getInfo().setWebUrl(command.getWebUrl());
		
		getInfo().setShortName(command.getShortName());
		getInfo().setSupplyType(command.getSupplyType());
		getInfo().setSupplyBoss(command.getSupplyBoss());
		getInfo().setEmail(command.getEmail());
		getInfo().setRem(command.getRem());
		

		
		if(address == null){
			setAddress(new EmAddress());
		}
		getAddress().setArea(command.getArea());
		getAddress().setCity(command.getCity());
		getAddress().setProvince(command.getProvince());
		getAddress().setDetail(command.getDetail());
		
		
		
		if(vendorStatus == null){
			setVendorStatus(new VendorStatus());
		}
		getVendorStatus().setIsClose(0);
		
		
		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
	}
	
	public void modify(SaveVendorCo command){
		
		if(info == null){
			setInfo(new VendorInfo());
		}
		getInfo().setVendorCode(command.getVendorCode());
		getInfo().setVendorName(command.getVendorName());
		getInfo().setFax(command.getFax());
		getInfo().setPostCode(command.getPostCode());
		getInfo().setTel(command.getTel());
		getInfo().setWebUrl(command.getWebUrl());
		getInfo().setShortName(command.getShortName());
		getInfo().setSupplyType(command.getSupplyType());
		getInfo().setSupplyBoss(command.getSupplyBoss());
		getInfo().setEmail(command.getEmail());
		getInfo().setRem(command.getRem());
		if(address == null){
			setAddress(new EmAddress());
		}
		getAddress().setArea(command.getArea());
		getAddress().setCity(command.getCity());
		getAddress().setProvince(command.getProvince());
		getAddress().setDetail(command.getDetail());
		
		if(getStatus()==null){
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());
	}

	public void remove(){
		getStatus().setRemove(true);
	}
	
	public VendorInfo getInfo() {
		return info;
	}


	public void setInfo(VendorInfo info) {
		this.info = info;
	}


	public EmAddress getAddress() {
		return address;
	}


	public void setAddress(EmAddress address) {
		this.address = address;
	}


	public BaseStatus getStatus() {
		return status;
	}


	public void setStatus(BaseStatus status) {
		this.status = status;
	}

	public VendorStatus getVendorStatus() {
		return vendorStatus;
	}

	public void setVendorStatus(VendorStatus vendorStatus) {
		this.vendorStatus = vendorStatus;
	}
	
}
